package com.sailfish.springbootdemo.service.db7.Impl;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.dao.db7.TbFlowProcessDetailDao;
import com.sailfish.springbootdemo.dao.db7.TbTemplateVersionDao;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db7.TbTemplateVersion;
import com.sailfish.springbootdemo.service.db7.TbTemplateVersionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sailfish.springbootdemo.utils.ResultUtil;

import java.util.List;

@Service
public class TbTemplateVersionServiceImpl implements TbTemplateVersionService {

    @Autowired
    private TbTemplateVersionDao tbTemplateVersionDao;

    @Autowired
    private TbFlowProcessDetailDao tbFlowProcessDetailDao;

    @Override
    public Result getTemplateVersionList(String data) {

        try {
            TbTemplateVersion param = new TbTemplateVersion();
            //解析Json字符串
            //判断是否为空
            String _template_type = "";
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);

                _template_type = jsonObject.getString("template_type");
                if (_template_type != null && !_template_type.isEmpty()) {
                    param.setTemplate_type(_template_type);
                }
            }
            if (StringUtils.isEmpty(_template_type)) {
                return ResultUtil.error(500, "template_type 不能为空", "", null);
            }

            List<TbTemplateVersion> list = tbTemplateVersionDao.getTemplateVersionList(param);

            /**
            //获取所有的模板记录
            List<TbFlowTemplate> listFlowTemplate = tbFlowTemplateDao.getFlowTemplateList();


            // 将 listFlowTemplate 转换为 Map，键为 flow_no，值为 TbFlowTemplate 对象
            Map<String, List<TbFlowTemplate>> flowTemplateMap = listFlowTemplate.stream()
                    .collect(Collectors.groupingBy(TbFlowTemplate::getFlow_no));

            // 遍历 list，对每个 TbTemplateVersion 对象，查找 flowTemplateMap 中对应的 TbFlowTemplate 对象列表，并赋值
            list.forEach(templateVersion -> {
                List<TbFlowTemplate> flowTemplates = flowTemplateMap.get(templateVersion.getFlow_no());
                templateVersion.setApproveList(flowTemplates);
            });
             */
            return ResultUtil.success(list);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }

    @Override
    public Result getNewestTemplateVersion(String data) {

        try {
            String _template_type = "";
            String _version= "";
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);

                _template_type = jsonObject.getString("template_type");
                _version= jsonObject.getString("version");
            }

            if (StringUtils.isEmpty(_template_type)) {
                return ResultUtil.error(500, "template_type 不能为空", "", null);
            }

            List<TbTemplateVersion> list = tbTemplateVersionDao.getNewestTemplateVersion(_template_type);

            if (_version != null && !_version.isEmpty()) {
                TbTemplateVersion temp=new  TbTemplateVersion();

                temp.setTemplate_type(_template_type);
                temp.setVersion(_version);

                List<TbTemplateVersion> list2 = tbTemplateVersionDao.getTemplateVersionList(temp);

                list.addAll(list2);
            }

            return ResultUtil.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }


    @Override
    public Result publishTemplateVersion(String data) {
        try {
            String _template_type = "";
            String _version= "";
            if (data != null && !data.isEmpty()) {
                JSONObject jsonObject = JSONObject.parseObject(data);

                _template_type = jsonObject.getString("template_type");
                _version= jsonObject.getString("version");
            }

            if (StringUtils.isEmpty(_template_type)) {
                return ResultUtil.error(500, "template_type 不能为空", "", null);
            }
            if (StringUtils.isEmpty(_version)) {
                return ResultUtil.error(500, "version 不能为空", "", null);
            }

            TbTemplateVersion temp=new  TbTemplateVersion();
            temp.setTemplate_type(_template_type);
            temp.setVersion(_version);
            //temp.setStatus(2);

            //校验当前版本是否可以分发,看是否审核完成了
            List<TbTemplateVersion> templateVersionList= tbTemplateVersionDao.getTemplateVersionList(temp);

            if(templateVersionList==null || templateVersionList.size()==0)
            {
                return ResultUtil.error(500, "当前版本不存在", "", null);
            }

            if(templateVersionList.get(0).getStatus()!=2)
            {
                return ResultUtil.error(500, "当前版本未审核通过", "", null);
            }

            temp.setStatus(3);
            Integer result= tbTemplateVersionDao.publishTemplateVersionStatus(temp);

            if(result==null || result==0)
            {
                return ResultUtil.error(500, "更新失败", "", null);
            }
            else
            {
                return ResultUtil.success("发布成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), "", null);
        }
    }
}
